package com.um.pasantiaum.cliente;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.um.pasantiaum.R;
import com.um.pasantiaum.objetos.Usuario;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PedidosAbiertosActivity extends FragmentActivity {
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	     Log.d("PCerr", "loaded");
	     setContentView(R.layout.historial1);
	    ArrayList <String> prods = new ArrayList<String>();
	    ArrayList<String> empresas = new ArrayList<String>();
	   
	    ParseQuery<ParseObject> query = ParseQuery.getQuery("Pedidos");
		query.whereEqualTo("usuario", ParseUser.getCurrentUser());
		query.whereEqualTo("estado", "open");
		query.include("empresa");
		//query.include("productos");
		Log.d("PCerr", ParseUser.getCurrentUser().getUsername());

		try {
			List<ParseObject> objects = query.find();
			Log.d("PCerr", String.valueOf(objects.size()));
			for (ParseObject po : objects) {
				String m = po.getString("productos");
				int i = po.getInt("costoTotal");
				ParseUser temp = po.getParseUser("empresa");
				Usuario u = new Usuario(temp);
				empresas.add(u.getName());
				prods.add("Empresa: " +  u.getName() + "\n" + " Prods: " + m + "\n" + " Precio Total: " + String.valueOf(i)); 
			}

		} catch (ParseException e1) {
			Log.d("Usuario", "Error: " + e1.getMessage());
		}
	

		  // Get ListView object from xml
       ListView listView = (ListView) findViewById(R.id.list);
        
        // Defined Array values to show in ListView
       
        // Define a new Adapter
        // First parameter - Context
        // Second parameter - Layout for the row
        // Third parameter - ID of the TextView to which the data is written
        // Forth - the Array of data

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, prods);


        // Assign adapter to ListView
        listView.setAdapter(adapter); 
        

	 /*   listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

	      @Override
	      public void onItemClick(AdapterView<?> parent, final View view,
	          int position, long id) {
	        final String item = (String) parent.getItemAtPosition(position);
	        view.animate().setDuration(2000).alpha(0)
	            .withEndAction(new Runnable() {
	              @Override
	              public void run() {
	                list.remove(item);
	                adapter.notifyDataSetChanged();
	                view.setAlpha(1);
	              }
	            });
	      }

	    });
	  }*/

	  class StableArrayAdapter extends ArrayAdapter<String> {

	    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

	    public StableArrayAdapter(Context context, int textViewResourceId,
	        List<String> objects) {
	      super(context, textViewResourceId, objects);
	      for (int i = 0; i < objects.size(); ++i) {
	        mIdMap.put(objects.get(i), i);
	      }
	    }

	    @Override
	    public long getItemId(int position) {
	      String item = getItem(position);
	      return mIdMap.get(item);
	    }

	    @Override
	    public boolean hasStableIds() {
	      return true;
	    }

	  }

	} 

}
